package com.ugoodtech.mdcc.web.controller;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved
         * Unauthorized copying of this file, via any medium is strictly prohibited
         * Proprietary and confidential
         * Written by Stone Shaw, 15/11/19
         */


import com.ugoodtech.mdcc.core.LogConstants;
import com.ugoodtech.mdcc.core.domain.UserLog;
import com.ugoodtech.mdcc.core.utils.BaseUtil;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.dto.LogResponse;
import com.ugoodtech.mdcc.web.service.UserLogService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(description = "用户日志获取接口")
@Controller
@RequestMapping("/userLog")
public class UserLogController extends BaseController{

    @Autowired
    private UserLogService userLogService;


    @ApiOperation("日志列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @ResponseBody
    public JsonResponse list(
            @ApiParam("开始时间") @RequestParam(value = "startDate", required = false) Long startDate,
            @ApiParam("结束时间") @RequestParam(value = "endDate", required = false) Long endDate,
            @ApiParam("人员姓名") @RequestParam(value = "userName", required = false) String userName,
            @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
            @ApiParam("操作类型") @RequestParam(value = "operateType", required = false) String operateType,
            @ApiParam("业务类型") @RequestParam(value = "businessType", required = false) String businessType,
            @ApiParam("报告类型") @RequestParam(value = "category", required = false) String category,
            @ApiParam("报告ID") @RequestParam(value = "reportId", required = false) Long reportId,
            @ApiParam("报告ID") @RequestParam(value = "ipAddress", required = false) String ipAddress,
            @ApiParam("是否审核通过") @RequestParam(value = "audited", required = false) Boolean audited,
            Pageable pageable
    ) throws Exception {
        isUserLogin();
        String originOperateType=null;
        String originBusinessType=null;
//        业务类型转换==================
        if(!StringUtils.isEmpty(businessType)){
            if (LogConstants.operate_type_confirm_action.equals(businessType)) {
                if (StringUtils.isEmpty(operateType)) {
                    originOperateType = "%" + LogConstants.operate_type_confirm_action + "%";
                }
            } else if (LogConstants.operate_type_send_export.equals(businessType)) {
                if (StringUtils.isEmpty(operateType)) {
                    originOperateType =LogConstants.operate_type_send_export;
                }
            } else if (LogConstants.operate_type_add_department_user.equals(businessType)) {
                if (StringUtils.isEmpty(operateType)) {
                    originOperateType = LogConstants.operate_type_add_department_user;
                }
            } else {
                originBusinessType = businessType;
            }
        }


//        操作类型转换==================
        if(!StringUtils.isEmpty(operateType)){
            if (operateType.startsWith(LogConstants.operate_type_send_export)) {
                originBusinessType=operateType.replace(LogConstants.operate_type_send_export,"");
                if (StringUtils.isEmpty(originOperateType)) {
                    originOperateType = LogConstants.operate_type_send_export;
                }
            } else {
                originOperateType = operateType;
            }
        }

        Page<UserLog> userLogPage = userLogService.queryUserLogs(startDate,
                endDate, userName, null, stationName, ipAddress, originOperateType, originBusinessType, null, category, reportId,audited, pageable);
        Page<UserLog> userLogs = BaseUtil.getDtoPage(userLogPage, pageable, LogResponse.class);
        return JsonResponse.successResponseWithPageData(userLogs);
    }

//    @ApiOperation("获取图片")
//    @RequestMapping(value = "/getImage", method = RequestMethod.GET)
//    @ResponseBody
//    public void getImage(
//            HttpServletResponse response,
//            @ApiParam("key") @RequestParam(value = "key") String key
//    ) throws Exception {
//        InputStream inputStream=AliServiceUtil.getObject(key,"finx");
//        String extensionName = "jpg";
//        response.setContentType("image/" + extensionName);
//        IOUtils.copy(inputStream, response.getOutputStream());
//    }

    Logger logger = LoggerFactory.getLogger(UserLogController.class);
}
